package breeze.pixel.weather.service.app_widget.configures;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;

import java.io.File;

import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;

public class Widget2ConfigActivity extends BaseWidgetConfigActivity{

    private View baseView;
    private ImageView imgBg;
    private File file;
    private int brightness = 0;
    private int transparency = 255;
    private int textColor = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imgBg = baseView.findViewById(R.id.wid_get_2_layout);
    }

    @Override
    protected int getWidgetMode() {
        return WIDGET_MODE_2;
    }

    @Override
    protected View returnChildView() {
        baseView = LayoutInflater.from(this).inflate(R.layout.widget_launcher_2,null);
        return baseView;
    }

    @Override
    protected void onPictureSelected(File file) {
        this.file = file;
        imgBg.setImageBitmap(AppToolUtil.roundedBitmap(
                brightness,
                BitmapFactory.decodeFile(file.getAbsolutePath()),
                50f));
    }

    @Override
    protected void onTransparentChanged(int val) {
        this.transparency = val;
        imgBg.setImageAlpha(val);
    }

    @Override
    protected void onBrightnessChanged(int val) {
        this.brightness = val;
        imgBg.setImageBitmap(AppToolUtil.roundedBitmap(
                brightness,
                BitmapFactory.decodeFile(file.getAbsolutePath()),
                50f));
    }

    @Override
    protected void onTextColorChanged(int val) {
        this.textColor = val;
        setTextColor(val);
    }

    private void setTextColor(int val) {
        ((TextView)baseView.findViewById(R.id.widget2_tem)).setTextColor(val);
        ((TextView)baseView.findViewById(R.id.widget2_data)).setTextColor(val);
        ((TextView)baseView.findViewById(R.id.widget2_date)).setTextColor(val);
        ((TextView)baseView.findViewById(R.id.widget2_cityname)).setTextColor(val);
    }

    @Override
    protected void onSave() {
        try {
            int widgetID = getWidgetID();
            if (file!=null) {
                File file = new File(AppToolUtil.getTempPicFile(this));
                file.renameTo(new File(AppToolUtil.getAppWidgetBackgroundPic_2(this)));
            }
            AppWidgetManager instance = AppWidgetManager.getInstance(this);
            RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget_launcher_2);

            if (new File(AppToolUtil.getAppWidgetBackgroundPic_2(this)).exists() && new File(AppToolUtil.getAppWidgetBackgroundPic_2(this)).length() > 0) {
                Bitmap bitmap = AppToolUtil.roundedBitmap(brightness,
                        BitmapFactory.decodeFile(AppToolUtil.getAppWidgetBackgroundPic_2(this)),
                        50f);
                rv.setImageViewBitmap(R.id.wid_get_2_layout, bitmap);
            } else {
                rv.setImageViewResource(R.id.wid_get_2_layout, R.drawable.cardview_tendp_white);
            }

            rv.setInt(R.id.wid_get_2_layout, "setImageAlpha", brightness);//背景透明度
            rv.setTextColor(R.id.widget2_tem, textColor);
            rv.setTextColor(R.id.widget2_data, textColor);
            rv.setTextColor(R.id.widget2_date, textColor);
            rv.setTextColor(R.id.widget2_cityname, textColor);

            instance.updateAppWidget(widgetID, rv);
            setResult(RESULT_OK,new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetID));
            finish();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void init() {

    }

    @Override
    public void initData() {

    }
}
